const err = require("../constant/err.type.js");
const { Gzgj, Poi, Updata, Nodes } = require("../common/file.js");
const { DATA_PATH,PORT,DOMAN,LX,LY } = require("../config/config.default.js");
const { getGaojing } = require("../common/common.js");
const { saveGaojings, getGaojings, getTerminalGaojing } = require("../services/gaojing.service.js");
const { saveLinks, getLinkByName } = require("../services/links.service.js");
const { getOptical } = require("../services/opticals.service.js")
const gzgj = new Gzgj(`${DATA_PATH}Js_GZGJ.json`);
const poi = new Poi(`${DATA_PATH}Js_POI.json`);
const updata = new Updata(`${DATA_PATH}Js_UpData.json`);
class Files {
    //刷新文件
    async updateJsonFile(ctx,next) {
        updata.update({ num: 1 })
        setTimeout(() => {
            updata.update({ num: 0 })
        }, 500);
        ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "ok", data: "" }), ctx);
        return
    }
    //创建和保存变电站
    async saveBDZ(ctx, next) {
        try {
            var res;
            var { ...station } = ctx.request.body;
            res = await poi.settingStation({ values: station });
            ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "ok", data: "" }), ctx);
            return;
        } catch (error) {
            console.log(error);
            ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "未知错误" + error, data: "" }), ctx);
            return;
        }
    }
    //删除所有变电站
    async jsonEraseAllStation(ctx,next){
        try {
            var res = await poi.emptyStation();
            ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "ok", data: "" }), ctx);
            return;
        } catch (error) {
            console.log(error);
            ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "未知错误" + error, data: "" }), ctx);
            return;
        }
    }
    //导入节点时，创建Js_node.json
    async jsonImportOpticals(ctx, next) {
        try {
            var res;
            var { linkId, linkName, arr } = ctx.request.body;
            if (linkId <= 0) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "获取JSON文件名编号失败", data: "" }), ctx);
                return;
            }
            res = await Nodes.createLine({ linkId, linkName, arr });
            if (res) {
                updata.update({ num: 1 });
                ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "写入JSON成功", data: "" }), ctx);
                return;
            } else {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "写入JSON失败", data: "" }), ctx);
                return;
            }
        } catch (error) {
            console.log(error);
            ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "未知错误" + error, data: "" }), ctx);
            return;
        }
    }
    //删除指定JS_Node.json文件
    async jsonEraseNodeJs(ctx, next) {
        try {
            var res;
            var { linkId } = ctx.request.body;
            if (!linkId || linkId < 0) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "获取JSON文件名编号失败", data: "" }), ctx);
                return;
            }
            res = await Nodes.eraseFile(linkId);
            if (res) {
                updata.update({ num: 1 });
                ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "删除JSON成功", data: "" }), ctx);
                return;
            } else {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "删除JSON失败", data: "" }), ctx);
                return;
            }

        } catch (error) {
            ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "未知错误" + error, data: "" }), ctx);
            return;
        }
    }
    //设置故障告警
    async setAlarm(ctx, next) {
        try {
            var res;
            var { list, gaojing } = ctx.request.body;
            if (!list || !(list instanceof Array) || list.length == 0) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "获取光缆信息失败", data: "" }), ctx);
                return;
            }
            if (gaojing.linkId <= 0) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "获取JSON文件名编号失败", data: "" }), ctx);
                return;
            }
            if (gaojing.linkName == "" || !gaojing.linkName) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "获取线路名称失败", data: "" }), ctx);
                return;
            }
            if (gaojing.status == "" || !gaojing.status) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "获取线路状态失败", data: "" }), ctx);
                return;
            }
            if (gaojing.statusValue == "" || !gaojing.statusValue) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "获取线路状态值失败", data: "" }), ctx);
                return;
            }
            //计算告警信息
            res = getGaojing(list, gaojing.statusValue);
            let url = ctx.request.host
            if (res.code == 0) {
                let point = { ID: `GZGJ${gaojing.gaojingId}`, Class: "GZ", Name: `BP_POI_GZGJ`, Show_Name: gaojing.linkName, Mesh: "SM_GZGJ", X: res.data.x, Y: res.data.y, Z: "8000", URL: `${DOMAN}:${PORT}/gzgj?gaojingId=${gaojing.gaojingId}`, Build: "1", Visible: "1", Click: "1", Init: "0", R: "1", G: "0", B: "0", Color: "",Cx:"0" };
                let p1 = JSON.parse(JSON.stringify(point));
                let p2 = JSON.parse(JSON.stringify(point));
                p1.Click = "1";
                p2.Click = "1";
                let as = await poi.settingAlarm({ gaojing: p1 });
                let ps =  JSON.parse(JSON.stringify(point));
                let wr = await gzgj.settingAlarm({ gaojing: p2});
                if (!wr) {
                    ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "生成Js_GZGJ失败", data: "" }), ctx);
                    return;
                }
                //改变线路颜色代码，需要linkId,linkName,originId
                Nodes.changeColor({ linkId: gaojing.linkId, linkName: gaojing.linkName, num: res.data.originId, model: 1 });
                if (as) {
                    setTimeout(async () => {
                        let secondPoint = JSON.parse(JSON.stringify(point));
                        secondPoint.Click = "0";
                        await poi.settingAlarm({ gaojing: secondPoint });
                        await gzgj.settingAlarm({ gaojing: secondPoint });
                        updata.update({ num: 1 })
                    }, 500);
                }
                ctx.status = 200;
                ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "ok", data: "" }), ctx);
                return;
            } else {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "计算告警信息时错误", data: "" }), ctx);
                return;
            }
        } catch (error) {
            console.log(error);
            ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "计算告警信息时错误", data: "" }), ctx);
            return;
        }
    }
    //故障定位 
    async dingwei(ctx, next) {
        try {
            var { linkName, gaojingId } = ctx.request.body;
            if (!gaojingId || gaojingId == undefined || gaojingId <= 0) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "告警ID错误", data: "" }), ctx);
                return;
            }
            if (!linkName || linkName == undefined || linkName == "") {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "线路名称错误", data: "" }), ctx);
                return;
            }
            let gaojing = await gzgj.getValueByCondition(linkName);
            if (!gaojing) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "读取Js_GZGJ.json失败", data: "" }), ctx);
                return;
            }
            gaojing.Click = "1";
            let pbj = await poi.getValueByCondition({ item: "gaojings", key: "Show_Name", value: linkName });
            if (!pbj) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "读取Js_POI.json失败", data: "" }), ctx);
                return;
            }
            pbj.Click = "1";
            await poi.settingAlarm({ gaojing: JSON.parse(JSON.stringify(pbj)) });
            await gzgj.settingAlarm({ gaojing: JSON.parse(JSON.stringify(gaojing)) });
            setTimeout(async () => {
                var secGaojing = JSON.parse(JSON.stringify(gaojing));
                secGaojing.Click = "0";
                var secPbj = JSON.parse(JSON.stringify(pbj));
                secPbj.Click = "0"
                await poi.settingAlarm({ vlaues: secPbj });
                await gzgj.settingAlarm({ values: secGaojing });
                updata.update({ num: 1 });
            }, 500)
            ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "OK", data: "" }), ctx);
            return;
        } catch (error) {
            console.log(error);
            ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: error, data: "" }), ctx);
            return;
        }
    }
    //解除告警
    async jiechu(ctx, next) {
        try {
            var { linkId, linkName, gaojingId } = ctx.request.body;
            if (!gaojingId || gaojingId == undefined || gaojingId <= 0) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "告警ID错误", data: "" }), ctx);
                return;
            }
            if (!linkName || linkName == undefined || linkName == "") {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "线路名称错误", data: "" }), ctx);
                return;
            }
            let gj = await getGaojings({ gaojingId });
                if (!gj) {
                    ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "没有找到可以匹配的告警数据", data: "" }), ctx);
                    return;
                }
            let gaojing = await gzgj.getValueByCondition(linkName);
            if (!gaojing) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "读取Js_GZGJ.json失败", data: "" }), ctx);
                return;
            }
            let pbj = await poi.getValueByCondition({ item: "gaojings", key: "Show_Name", value: linkName });
            if (!pbj) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "读取Js_POI.json失败", data: "" }), ctx);
                return;
            }
            if(gaojing['Cx'] > 0){
                gaojing['URL'] = gaojing['URL'].slice(0,-4)
            }
            gaojing['Init'] = "1";
            gaojing['X'] = "0";
            gaojing['Y'] = "0";
            gaojing['Z'] = "8000000";
            gaojing['Click'] = "0";
            gaojing['Cx'] = "0"
            if(pbj['Cx'] > 0){
                pbj['URL'] = pbj['URL'].slice(0,-4)
            }
            pbj['Init'] = "1";
            pbj['X'] = "0";
            pbj['Y'] = "0";
            pbj['Z'] = "8000000";
            pbj['Click'] = "0";
            pbj['Cx'] = "0";
            let g1 = JSON.parse(JSON.stringify(gaojing));
            let p1 = JSON.parse(JSON.stringify(pbj));
            await poi.settingAlarm({ gaojing: pbj });
            await gzgj.settingAlarm({ gaojing: gaojing });
            setTimeout(async () => {
                g1['Init'] = "0";
                p1['Init'] = "0";
                await poi.settingAlarm({ gaojing: p1 });
                await gzgj.settingAlarm({ gaojing: g1 });
            }, 500);//解除故障时，0.5秒后init就成0
            await Nodes.changeColor({ linkId, linkName, num: gj.originId, model: 2 });
            updata.update({ num: 1 })
            ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "OK", data: "" }), ctx);
        } catch (error) {
            console.log(error);
            ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "未知错误" + error, data: "" }), ctx);
            return;
        }
    }
    //重现告警
    async createGzcx(ctx, next) {
        try {
            /* var { ...gaojing } = ctx.request.body;
            gaojing.Click = "1";
            poi.setGzcx(gaojing);
            setTimeout(() => {
                gaojing.Click = "0";
                poi.setGzcx(gaojing);
            }, 500);
            updata.update({ num: 1 })
            setTimeout(() => {
                gaojing.Click = "0";
                poi.setGzcx(gaojing);
            }, 1000); */
            var res;
            var {...gaojing} = ctx.request.body;
            if(!gaojing.linkName){
                ctx.app.emit("error",err.setDefaultValue({code:-1,msg:"线路名称为空",data:gaojing}),ctx);
                return;
            }
            if (gaojing.linkId <= 0) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "获取JSON文件名编号失败", data: "" }), ctx);
                return;
            }
            if (gaojing.status == "" || !gaojing.status) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "获取线路状态失败", data: "" }), ctx);
                return;
            }
            if (gaojing.statusValue == "" || !gaojing.statusValue) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "获取线路状态值失败", data: "" }), ctx);
                return;
            }
            var list = await getOptical({ linkName:gaojing.linkName });
            if (!list || !(list instanceof Array) || list.length == 0) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "获取坐标信息错误", data: "" }), ctx);
                return;
            }
            //计算告警信息
            res = getGaojing(list, gaojing.statusValue);
            let url = ctx.request.host
            if(res.code == 0){
                let point = { ID: `GZGJ${gaojing.gaojingId}`, Class: "GZ", Name: `BP_POI_GZGJ`, Show_Name: gaojing.linkName, Mesh: "SM_GZGJ", X: res.data.x, Y: res.data.y, Z: "8000", URL: `${DOMAN}:${PORT}/gzgj?gaojingId=${gaojing.gaojingId}&cx=1`, Build: "1", Visible: "1", Click: "1", Init: "0", R: "1", G: "0", B: "0", Color: "",Cx:"1" };
                let p1 = JSON.parse(JSON.stringify(point));
                let p2 = JSON.parse(JSON.stringify(point));
                p1.Click = "1";
                p2.Click = "1";
                let as = await poi.settingAlarm({ gaojing: p1 });
                let ps =  JSON.parse(JSON.stringify(point));
                let wr = await gzgj.settingAlarm({ gaojing: p2});
                if (!wr) {
                    ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "生成Js_GZGJ失败", data: "" }), ctx);
                    return;
                }
                //改变线路颜色代码，需要linkId,linkName,originId
                Nodes.changeColor({ linkId: gaojing.linkId, linkName: gaojing.linkName, num: res.data.originId, model: 1 ,color:1});
                if (as) {
                    setTimeout(async () => {
                        let secondPoint = JSON.parse(JSON.stringify(point));
                        secondPoint.Click = "0";
                        await poi.settingAlarm({ gaojing: secondPoint });
                        await gzgj.settingAlarm({ gaojing: secondPoint });
                        updata.update({ num: 1 })
                    }, 500);
                }
                ctx.status = 200;
                ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "ok", data: "" }), ctx);
                return;
            }else{
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "计算告警信息时错误", data: "" }), ctx);
                return;
            }
        } catch (error) {
            console.log(error);
            ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "未知错误" + error, data: "" }), ctx);
            return;
        }
    }
    //清除重现告警
    async clearGzcx(ctx, next) {
        try {
            var { ...gaojing } = ctx.request.body;
            gaojing.Init = "1";
            poi.setGzcx(gaojing);
            setTimeout(() => {
                gaojing.Init = "0";
                poi.setGzcx(gaojing);
            }, 500);
            updata.update({ num: 1 })
            setTimeout(() => {
                gaojing.Init = "0";
                poi.setGzcx(gaojing);
            }, 1000);
            ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "OK", data: "" }), ctx);
        } catch (error) {
            console.log(error);
            ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "未知错误" + error, data: "" }), ctx);
            return;
        }
    }
    //在Js_POI.json创建line
    async jsonCreateLine(ctx, next) {
        try {
            var { linkId, linkName, arr } = ctx.request.body;
            if (!linkId) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "线路ID错误", data: "" }), ctx);
                return;
            }
            if (!linkName) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "线路名称错误", data: "" }), ctx);
                return;
            }
            if (!(arr instanceof Array) || arr.length < 2) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "没有可添加的节点", data: "" }), ctx);
                return;
            }
            var len = arr.length;
            let url = ctx.request.host;
            let num = len % 2 == 0 ? len / 2 - 1 : Math.floor(len / 2) - 1;
            let x = 3836187.737 * (0 + arr[num]['longitude'] - LX) / 0.86915612;
            let y = -7091297.591 * (0 + arr[num]['latitude'] - LY) / 1.20283173;
            let R = arr[num]['R'];
            let G = arr[num]['G'];
            let B = arr[num]['B'];
            let obj = { ID: `Line${linkId}`, Class: "Line", Name: `BP_POI_Line${linkId}`, Show_Name: linkName, Mesh: "SM_Line", X: x.toString(), Y: y.toString(), Z: "5000", URL: `http://${url}/line?linkId=${linkId}`, Build: "1", Visible: "1", Click: "0", Init: "0", Color: "Green", R: R, G: G, B: B }
            await poi.settingLines({ values: obj, key: "Show_Name" });
            await updata.update({ num: 1 });
            ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "写入Js_POI.json成功", data: "" }), ctx);
            return;
        } catch (error) {
            console.log(error);
            ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "未知错误" + error, data: "" }), ctx);
            return;
        }
    }
    //删除坐标信息时，修改POI
    async lineEraseNode(ctx, next) {
        try {
            var { linkId, linkName } = ctx.request.body;
            if (!linkId) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "线路ID错误", data: "" }), ctx);
                return;
            }
            if (!linkName) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "线路名称错误", data: "" }), ctx);
                return;
            }
            let pbj = await poi.getValueByCondition({ item: "lines", key: "Show_Name", value: linkName });
            if (!pbj) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "读取Js_GZGJ.json失败", data: "" }), ctx);
                return;
            }
            pbj['Build'] = "0";

            await poi.settingLines({ values: pbj, key: "Show_Name" });
            await updata.update({ num: 1 });
            ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "写入Js_POI.json成功", data: "" }), ctx);
            return;
        } catch (error) {
            console.log(error);
            ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "未知错误" + error, data: "" }), ctx);
            return;
        }
    }
    //删除线时，修改POI
    async lineEraseLink(ctx, net) {
        try {
            var { linkId, linkName } = ctx.request.body;
            if (!linkId) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "线路ID错误", data: "" }), ctx);
                return;
            }
            if (!linkName) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "线路名称错误", data: "" }), ctx);
                return;
            }
            let pbj = await poi.getValueByCondition({ item: "lines", key: "Show_Name", value: linkName });
            if (!pbj) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "读取Js_GZGJ.json失败", data: "" }), ctx);
                return;
            }
            pbj['Init'] = "1";
            pbj['X'] = "0";
            pbj['Y'] = "0";
            pbj['Z'] = "80000000";
            await poi.settingLines({ values: pbj, key: "Show_Name" });
            await updata.update({ num: 1 });
            ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "写入Js_POI.json成功", data: "" }), ctx);
            return;
        } catch (error) {
            console.log(error);
            ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "未知错误" + error, data: "" }), ctx);
            return;
        }
    }
    //gllist页面点击线路修改visible和click属性
    async hitLine(ctx, next) {
        try {
            var res;
            var { linkId, linkName } = ctx.request.body;
            if (linkId <= 0) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "线路ID错误", data: "" }), ctx);
                return;
            }
            if (!linkName) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "线路名称错误", data: "" }), ctx);
                return;
            }
            let pbj = await poi.getValueByCondition({ item: "lines", key: "Show_Name", value: linkName });
            if (!pbj) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "读取Js_POI.json失败", data: "" }), ctx);
                return;
            }
            await poi.hitLine({ values: { Show_Name: linkName,Click:"1" }});
            setTimeout(async () => {
                await poi.hitLine({ values:{Show_Name:linkName,Click:"0"}});
            }, 500)
            setTimeout(async () => {
                await poi.hitLine({ values:{Show_Name:linkName,Click:"0"}});
            }, 1000)
            ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "写入Js_POI.json成功", data: "" }), ctx);
            return;
        } catch (error) {
            console.log(error);
            ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "未知错误" + error, data: "" }), ctx);
            return;
        }
    }
    //设置poi.josn中的show_name
    async setShowName(ctx, next) {
        try {
            var res;
            var { linkId, linkName } = ctx.request.body;
            if (!linkId || linkId <= 0) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "linkId错误", data: "" }), ctx);
                return;
            }
            if (!linkName || linkName <= 0) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "线路名称错误", data: "" }), ctx);
                return;
            }
            let pbj = await poi.getValueByCondition({ item: "lines", key: "Show_Name", value: linkName });
            if (!pbj) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "读取Js_GZGJ.json失败,请检查线路名称是否正确", data: "" }), ctx);
                return;
            }
            ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "写入Js_POI.json成功", data: "" }), ctx);
            return;

        } catch (error) {
            console.log(error);
            ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "未知错误" + error, data: "" }), ctx);
            return;
        }
    }
    async autoGzgj(ctx, netx) {
        try {
            var obj = ctx.request.body;
            if (!obj.linkName) {
                ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "参数错误", data: obj }), ctx);
                return;
            }
            if (!obj.alarmType || obj.alarmType < 0) {
                ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "告警类型编码错误", data: obj.alarmType }), ctx);
                return;
            }
            if (!obj.alarmDetails || obj.alarmDetails <= 0) {
                ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "告警详细内容错误", data: obj.alarmDetails }), ctx);
                return;
            }
            if (!obj.alarmDescribe.latitude) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "纬度错误", data: obj.alarmDetails }), ctx);
                return;
            }
            if (!obj.alarmDescribe.longitude) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "经度错误", data: obj.alarmDetails }), ctx);
                return;
            }
            let lk = await getLinkByName(obj.linkName);
            if (!lk) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: '查找链路失败', data: "" }), ctx)
            }
            let data = {
                linkName: lk.linkName,
                linkId: lk.linkId,
                Longitude: obj.alarmDescribe.longitude,
                Latitude: obj.alarmDescribe.latitude,
                lineName: lk.lineName,
                times: lk.times,
                cancat: lk.cancat,
                works: lk.works
            }
            if (obj.alarmType == 50 || obj.alarmType == 6 || obj.alarmType == 54) {
                data.status = "中断";
            } else if (obj.alarmType == 51 || obj.alarmType == 53 || obj.alarmType == 56 || obj.alarmType == 57) {
                data.status = "正常"
            } else if (obj.alarmType == 52) {
                data.status = "衰减"
            } else {
                //data.status = "中断"
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "故障类型识别错误", data: obj.alarmType }), ctx);
                return;
            }
            if (lk.status == data.status) {
                if (obj.alarmDescribe.position > 0) {
                    //存在arlarmDescribe.position时，代表是断路或衰减，这时候，如果position不同的时候，继续报警，如果相同，则不报警
                    if (obj.alarmDescribe.position == lk.statusValue) {
                        ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "针对本线路操作重复，忽略此操作", data: obj.alarmType }), ctx);
                        return;
                    }
                } else {
                    ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "针对本线路操作重复，忽略此操作", data: obj.alarmType }), ctx);
                    return;
                }
            }
            if (obj.alarmDescribe.position) {
                data.statusValue = obj.alarmDescribe.position;
            }
            var link = await getLinkByName(data.linkName);
            if (data.status == "正常") {
                //解除报警
                let gj = await getTerminalGaojing({ linkName: data.linkName, gaojingType: 1 });
                if (!gj) {
                    ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "没有找到可以匹配的告警数据", data: "" }), ctx);
                    return;
                }
                //保存告警信息
                let tmp = await saveGaojings({ gaojingId: gj.gaojingId, gaojingType: 1, status: data.status, statusValue: data.statusValue });
                if (!tmp) {
                    ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "保存告警信息失败", data: "" }), ctx);
                    return;
                }
                let gaojing = await gzgj.getValueByCondition(data.linkName);
                if (!gaojing) {
                    ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "读取Js_GZGJ.json失败", data: "" }), ctx);
                    return;
                }
                let pbj = await poi.getValueByCondition({ item: "gaojings", key: "Show_Name", value: data.linkName });
                if (!pbj) {
                    ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "读取Js_GZGJ.json失败", data: "" }), ctx);
                    return;
                }
                gaojing['Init'] = "1";
                gaojing['X'] = "0";
                gaojing['Y'] = "Y";
                gaojing['Z'] = "8000000";
                gaojing['Click'] = "0";
                pbj['Init'] = "1";
                pbj['X'] = "0";
                pbj['Y'] = "Y";
                pbj['Z'] = "8000000";
                pbj['Click'] = "0";
                let g1 = JSON.parse(JSON.stringify(gaojing));
                let p1 = JSON.parse(JSON.stringify(pbj));
                await poi.settingAlarm({ gaojing: pbj, key: "ID" });
                await gzgj.settingAlarm({ gaojing: gaojing });
                setTimeout(async () => {
                    g1['Init'] = "0";
                    g1['Click'] = "0";
                    p1['Init'] = "0";
                    p1['Click'] = "0";
                    await poi.settingAlarm({ vlaues: p1, key: "ID" });
                    await gzgj.settingAlarm({ values: g1 });
                }, 500);
                //改变线路颜色代码，需要linkId,linkName,originId
                await Nodes.changeColor({ linkId: link.linkId, linkName: link.linkNme, num: gj.originId, model: 2 })
                updata.update({ num: 1 });
                if (link instanceof Object) {
                    let ss = await saveLinks({ linkId: link.linkId, status: "正常", statusValue: "" });
                    if (!ss) {
                        ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "保存线路信息失败", data: "" }), ctx);
                        return;
                    }
                } else {
                    ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "保存线路信息失败", data: "" }), ctx);
                    return;
                }
                ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "OK", data: "" }), ctx);
            } else {
                //设置报警
                var res;
                if (!data.linkName) {
                    ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "线路名称错误 ", data: "" }), ctx);
                    return;
                }
                let pd = { linkId: link.linkId, status: data.status, statusValue: data.statusValue }
                let ss = await saveLinks(pd);
                if (!ss) {
                    ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "保存线路信息失败1", data: "" }), ctx);
                    return;
                }
                var rs = await getOptical({ linkName: data.linkName });
                if (!rs || !(rs instanceof Array) || rs.length == 0) {
                    ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "获取坐标信息错误", data: "" }), ctx);
                    return;
                }
                let list = rs;
                //计算告警信息
                res = getGaojing(list, data.statusValue,obj.alarmDescribe.longitude,obj.alarmDescribe.latitude);
                let url = ctx.request.host;
                if (res.code == 0) {                    
                    //设备告警信息
                    let obj = { type: res.data.type, origin: res.data.origin, finishing: res.data.finishing, finishingId: res.data.finishingId, distance: res.data.distance, X: res.data.x, Y: res.data.y, linkId: link.linkId, linkName: link.linkName, lineName: link.lineName, times: link.times, status: data.status, statusValue: data.statusValue, cancat: link.cancat, works: link.works };
                    let gjdata = { origin: res.data.origin, finishing: res.data.finishing, finishingId: res.data.finishingId, distance: res.data.distance, X: res.data.x, Y: res.data.y, linkId: link.linkId, linkName: link.linkName, lineName: link.lineName, times: link.times, status: data.status, statusValue: data.statusValue, cancat: link.cancat, works: link.works,longitude:res.data.longitude,latitude:res.data.latitude }
                    gjdata.works = JSON.parse(gjdata.works);
                    let gjrs = await saveGaojings(gjdata);
                    if (!gjrs) {
                        ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "保存告警信息失败", data: "" }), ctx);
                        return;
                    }
                    let point = { ID: `GZGJ${gjrs.gaojingId}`, Class: "GZ", Name: `BP_POI_GZGJ1`, Show_Name: link.linkName, Mesh: "SM_GZGJ", X: res.data.x, Y: res.data.y, Z: "8000", URL: `${DOMAN}:${PORT}/gzgj?gaojingId=${gjrs.gaojingId}`, Build: "1", Visible: "1", Click: "1", Init: "0", R: "1", G: "0", B: "0", Color: "",Cx:"0" }
                    var point1 = JSON.parse(JSON.stringify(point));
                    var point2 = JSON.parse(JSON.stringify(point));
                    point1['Click'] = "1";
                    let ps = await poi.settingAlarm({ gaojing: point1 });
                    if (!ps) {
                        ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "生成Js_POI.json失败", data: "" }), ctx);
                        return;
                    }
                    let wr = await gzgj.settingAlarm({ gaojing: point });
                    if (!wr) {
                        ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "生成Js_GZGJ.json失败", data: "" }), ctx);
                        return;
                    }
                    //填写改变颜色代码
                    Nodes.changeColor({ linkId: link.linkId, linkName: link.linkNme, num: res.data.originId, model: 1 })
                    setTimeout(async () => {
                        point2.Click = "0";
                        await poi.settingAlarm({ gaojing: point2 });
                        await gzgj.settingAlarm({ gaojing: point2 });
                        updata.update({ num: 1 });
                    }, 500)
                    ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "写入JSON成功", data: "" }), ctx);
                    return;
                } else {
                    ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: res.msg, data: res.data }), ctx);
                    return;
                }
            }
        } catch (error) {
            console.log(error);
            ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "未知错误", data: obj.alarmType }), ctx);
            return;
        }
    }
    async jsonCreateBOX(ctx, next) {
        try {
            var { linkId, arr } = ctx.request.body;
            if (!linkId || linkId <= 0) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "线路ID错误", data: "" }), ctx);
                return;
            }
            if (!arr || !(arr instanceof Array) || arr.length == 0) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "数据不能为空", data: "" }), ctx);
                return;
            }
            await poi.settingBoxes({ arr: arr, linkId });
            ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "ok", data: "" }), ctx);
        } catch (error) {
            console.log(error);
            ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "创建box失败，" + error, data: "" }), ctx)
        }
    }
    async jsonEraseBOX(ctx, next) {
        try {
            var { linkId } = ctx.request.body;
            if (!linkId || linkId <= 0 || linkId == undefined) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "线路ID错误", data: "" }), ctx);
                return;
            }
            await poi.clearBoxes({ linkId });
            ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "ok", data: "" }), ctx);
        } catch (error) {
            console.log(error);
            ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "创建box失败，" + error, data: "" }), ctx)
        }
    }
    async jsonCreateBDZ(ctx, next) {
        try {
            var { arr } = ctx.request.body;
            if (!arr || !(arr instanceof Array) || arr.length == 0) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "数据不能为空", data: "" }), ctx);
                return;
            }
            await poi.importStation({ arr });
            ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "ok", data: "" }), ctx);
        } catch (error) {
            console.log(error);
            ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "创建box失败，" + error, data: "" }), ctx)
        }
    }
    async jsonEraseBDZ(ctx, next) {
        try {
            var { } = ctx.request.body;
            if (!linkId || linkId <= 0 || linkId == undefined) {
                ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "线路ID错误", data: "" }), ctx);
                return;
            }
            await poi.settingStation({ linkId });
            ctx.app.emit("error", err.setDefaultValue({ code: 0, msg: "ok", data: "" }), ctx);
        } catch (error) {
            console.log(error);
            ctx.app.emit("error", err.setDefaultValue({ code: -1, msg: "创建box失败，" + error, data: "" }), ctx)
        }
    }
}
module.exports = new Files();